<template>
  <div class="bigBox">
    <borderomponent>
      <dialogXinzengchengyuan
        :isdialogVisibleNew="isDialogVisible"
        @jianting="jianting"
        @name="getName"
      ></dialogXinzengchengyuan>
      <selectPersonPopup
        @getHeadName="getHeadName"
        @addMembers="addMembers"
        @close="clsoe"
        :isOuterVisible="isOuterVisible"
      ></selectPersonPopup>
      <div class="maintenanceInformationWrapper">
        <div class="maintenanceInformationGroup">
          <el-form
            :model="baseFrom"
            :rules="rules"
            ref="baseFromRef"
            label-width="auto"
            class="miBaseForm"
          >
            <div class="binanBox">
              <el-form-item label="避难场所名称:">
                <div class="miTxt">
                  {{ baseFrom.refugeName }}
                </div>
              </el-form-item>
              <el-form-item label="避难场所类型:">
                <div class="miTxt">
                  {{ baseFrom.refugeType||"--" }}
                </div>
              </el-form-item>
              <el-form-item
                label="避难场所地址:"
                prop="address"
                hide-required-asterisk="true"
                :rules="{
                  required: true,
                  message: '请输入地址',
                  trigger: 'blur'
                }"
              >
                <!-- <el-input
                  placeholder="请输入关键词选择地址"
                  v-model="baseFrom.address"
                  style="width: 200px"
                >
                  <template slot="append">
                    <div class="append-wrapper append-cup">
                      <div class="append-txt">选择地址</div>
                      <i class="el-icon el-icon-address-outline"></i>
                    </div>
                  </template>
                </el-input> -->
                <div class="miTxt" v-if="!isEdit">
                  {{ baseFrom.address }}
                </div>
                <NewMap
               class="miTxt"
                  v-if="isEdit"
                  @childByValue="childValue"
                  @getAddress="getAddress"
                  :Newaddress="Newaddress"
                ></NewMap>
              </el-form-item>
            </div>
            <div class="binanBox">
              <el-form-item label="负责人:" prop="headName">
                <el-input
                  disabled
                  v-if="isEdit"
                  placeholder="请选择负责人"
                  v-model="baseFrom.headName"
                  style="width: 200px"
                >
                  <template slot="append">
                    <div
                      class="append-wrapper append-cup"
                      @click="tianjia(true)"
                    >
                      <div class="append-txt">添加成员</div>
                      <i class="el-icon el-icon-add-user"></i>
                    </div>
                  </template>
                </el-input>
                <div class="miTxt" v-if="!isEdit">
                  {{ baseFrom.headName }}
                </div>
              </el-form-item>
              <el-form-item
                label="负责人电话:"
                prop="headPhone"
                hide-required-asterisk="true"
                :rules="[
                  {
                    required: true,
                    message: '负责人电话',
                    trigger: 'blur'
                  },
                  {
                    min: 11,
                    max: 11,
                    message: '请输入11位手机号',
                    trigger: 'blur'
                  }
                ]"
              >
                <el-input
                  v-if="isEdit"
                  placeholder="请输入负责人电话"
                  v-model="baseFrom.headPhone"
                ></el-input>
                <div class="miTxt" v-if="!isEdit">
                  {{ baseFrom.headPhone }}
                </div>
              </el-form-item>
              <el-form-item label="应急避难场所占地总面积:" prop="area">
                <el-input
                  placeholder="请输入"
                  v-model="baseFrom.area"
                  v-if="isEdit"
                >
                  <template slot="append">
                    <div class="append-wrapper">
                      <div class="append-txt">㎡</div>
                    </div>
                  </template>
                </el-input>
                <div class="miTxt" v-if="!isEdit">
                  {{ baseFrom.area }}
                </div>
              </el-form-item>
            </div>
            <div class="binanBox">
              <el-form-item label="应急避难场所容纳人数:" prop="peopleNum">
                <el-input
                  v-if="isEdit"
                  placeholder="请输入"
                  v-model="baseFrom.peopleNum"
                  style="width: 200px"
                >
                  <template slot="append">
                    <div class="append-wrapper">
                      <div class="append-txt">人</div>
                    </div>
                  </template>
                </el-input>
                <div class="miTxt" v-if="!isEdit">
                  {{ baseFrom.peopleNum }}
                </div>
              </el-form-item>
              <el-form-item
                label="日常专职维护或管理人员数量:"
                prop="managerNum"
              >
                <el-input
                  placeholder="请输入"
                  v-model="baseFrom.managerNum"
                  style="width: 200px"
                  v-if="isEdit"
                >
                  <template slot="append">
                    <div class="append-wrapper">
                      <div class="append-txt">人</div>
                    </div>
                  </template>
                </el-input>
                <div class="miTxt" v-if="!isEdit">
                  {{ baseFrom.managerNum }}
                </div>
              </el-form-item>
              <el-form-item>
                <div style="width: 200px"></div>
              </el-form-item>
            </div>
          </el-form>
          <div class="baseFromBtn">
            <el-button
              @click="bianji"
              v-if="!isEdit"
              style="
                border: 1px solid #38c2c6;
                background: #38c2c6;
                border-radius: 4px;
                font-size: 16px;
                font-family: MicrosoftYaHei;
                color: rgba(255, 255, 255, 0.9);
              "
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              v-if="isEdit"
              @click="Back"
              class="table-cancel-btn"
              >返回
            </el-button>
            <el-button
              @click="submitForm('baseFromRef')"
              v-if="isEdit"
              style="
                border: 1px solid #38c2c6;
                background: #38c2c6;
                border-radius: 4px;
                font-size: 16px;
                font-family: MicrosoftYaHei;
                color: rgba(255, 255, 255, 0.9);
              "
              >保存</el-button
            >
          </div>
        </div>
      </div>
    </borderomponent>
  </div>
</template>

<script>
import borderomponent from '@/components/borderomponent/index.vue'
import { checkTel, checkNumber, checkDecimalOrNumber } from '@/utils/rules'
import dialogXinzengchengyuan from '@/components/dialog-xinzengchengyuan.vue'
import selectPersonPopup from '@/components/selectPersonPopup/index.vue'
import {
  refugeGetByDepId,
  refugeSaveOrUpdate,
  idaasFindUsersByDepId,
  organizationListTree
} from '@/api/resourceManagement/index.js'
import NewMap from '@/components/egis-map/map.vue'
export default {
  name: 'TeamAdministrator',
  components: {
    borderomponent,
    dialogXinzengchengyuan,
    selectPersonPopup,
    NewMap
  },
  props: {
    placeInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      isEdit: false,
      Newaddress: '',
      isOuterVisible: false,
      isDialogVisible: 0,
      renyuan: '',
      renyuanType: '',
      dialogVisible: false,
      filterText: '',
      renyuanfilterText: '',
      data: [],
      renyaundata: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      baseFrom: {
        id: '',
        depId: '', //部门id，暂时自填
        refugeName: '', //避难场所名称
        refugeType: '', //避难场所类型
        address: '', //驻地位置
        coordinateX: '', //x坐标
        coordinateY: '', //y坐标
        headName: '', //责任人
        headPhone: '', //责任人电话
        peopleNum: '', //应急避难场所容纳人数
        managerNum: '', //日常专职维护或管理人员数量
        area: '' //面积
      },
      rules: {
        address: [
          {
            required: true,
            message: '请输入避难场所地址',
            trigger: ['blur']
          }
        ],
        headName: [
          {
            required: true,
            message: '请输入负责人',
            trigger: ['blur']
          }
        ],
        headPhone: [
          {
            required: true,
            message: '请输入负责人电话',
            trigger: ['blur']
          },
          {
            validator: checkTel,
            trigger: ['blur']
          }
        ],
        area: [
          {
            required: true,
            message: '请输入应急避难场所占地总面积',
            trigger: ['blur']
          }
          // {
          //   validator: checkDecimalOrNumber,
          //   trigger: ['blur']
          // }
        ],
        peopleNum: [
          {
            required: true,
            message: '请输入应急避难场所容纳人数',
            trigger: ['blur']
          }
          // {
          //   validator: checkNumber,
          //   trigger: ['blur']
          // }
        ],
        managerNum: [
          {
            required: true,
            message: '请输入日常专职维护或管理人员数量',
            trigger: ['blur']
          }
          // {
          //   validator: checkNumber,
          //   trigger: ['blur']
          // }
        ]
      }
    }
  },
  mounted() {
    this.refugeGetByDepId()
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    },
    renyuanfilterText(val) {
      this.$refs.newtree.filter(val)
    },
    newdialogVisible(val) {
      console.log(val)
      this.dialogVisible = val
    },

    $route(to, from) {
      window.location.reload() // 这个方法可行 直接就强制刷新
    }
  },
  methods: {
    Back() {
      this.isEdit = false
    },
    bianji() {
      this.isEdit = true
    },
    getAddress(e) {
      this.baseFrom.address = e
    },
    clsoe() {
      this.isOuterVisible = false
    },
    // 开启成员选择
    tianjia(e) {
      this.isOuterVisible = e
      console.log(this.isOuterVisible)
    },
    // 新增成员
    addMembers() {
      this.isOuterVisible = false
      this.isDialogVisible = true
    },
    // 获取负责人
    getHeadName(e) {
      this.isOuterVisible = false
      console.log(e.displayName)
      this.$set(this.baseFrom, 'headName', e.displayName)
      this.$set(this.baseFrom, 'headPhone', e.phoneNum)
      this.$set(this.baseFrom, 'headId', e.id)
      console.log(e.displayName, this.baseFrom.headName)
    },
    getName(e) {
      this.filterText = ''
      this.renyuanfilterText = ''
      this.baseFrom.headName = e.name
      this.baseFrom.headPhone = e.phone
      this.baseFrom.headId = e.id
      this.dialogVisible = false
    },
    jianting(e) {
      this.isDialogVisible = 0
    },
    xinzeng() {
      console.log('新增成员')
      this.isDialogVisible = 1
      this.dialogVisible = false
    },
    childValue(e) {
      console.log(e,"变化")
      this.Newaddress = e
      this.Newaddress.address = this.baseFrom.address
      // this.baseFrom.address = e.address
      this.baseFrom.coordinateX = e.latitude
      this.baseFrom.coordinateY = e.longitude
      console.log(this.baseFrom)
    },
    handleClose() {
      this.dialogVisible = false
      console.log(12234, this.dialogVisible)
      this.$forceUpdate()
    },
    newdialogVisible(e) {
      console.log(e)
      this.filterText = ''
      this.renyuanfilterText = ''
      if (e == 1) {
        this.dialogVisible = false
      } else {
        this.baseFrom.headName = this.renyuan
        console.log(e)
        this.dialogVisible = false
      }
    },
    filterNode(value, data) {
      console.log(data)
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    handleNodeClick(data) {
      console.log(data)
      if (data.children.length == 0) {
        idaasFindUsersByDepId({ depId: data.id }).then(res => {
          console.log(res)
          this.renyaundata = JSON.parse(
            JSON.stringify(res.data).replace(/name/g, 'label')
          )
        })
      }
    },
    renyuanHandleNodeClick(data) {
      console.log(data.label)
      this.renyuan = data.label
    },
    refugeGetByDepId() {
      let data = { depId: this.$userInfo.orgId }
      refugeGetByDepId(data).then(res => {
        this.baseFrom = res.data
      })
    },
    submitForm(formName) {
      if (this.baseFrom.coordinateX == '' || this.baseFrom.coordinateY == '') {
        this.$message({
          message: '请选择经纬度',
          type: 'warning'
        })
        return
      }
      console.log(formName)
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log('提交接口')
          this.baseFrom.depId = this.$userInfo.orgId
          this.baseFrom.areaCode = this.$userInfo.areaCode
          let data = this.baseFrom
          refugeSaveOrUpdate(data).then(res => {
            console.log('提交接口', res)
            if (res && res.code == 0) {
              this.isEdit = false
              this.$message.success('保存成功')
            } else {
              this.$message.error('服务端返回数据异常')
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss">
.bigBox {
  width: 100%;
  height: 100%;
  .maintenanceInformationWrapper {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    .maintenanceInformationGroup {
      box-sizing: border-box;
      min-height: 100%;
      padding-bottom: 80px;
      position: relative;
      .baseFromBtn {
        position: absolute;
        left: 50%;
        bottom: 17px;
        transform: translateX(-50%);
        .el-button--default {
          background-color: rgba(56, 194, 198, 1);
        }
        .el-button--primary {
          background-color: rgba(29, 90, 143, 1);
        }
      }
      .miTxt {
        width: 260px;
        color: #cbe7ff;
      }
    }
  }
}
.el-icon-add-user {
  width: 14px;
  height: 14px;
  background-image: url(~@/assets/icons/input_box-contacts@2x.png);
  background-size: 100% 100%;
  background-repeat: no-repeat;
  background-position: center center;
}
.append-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  &.append-cup {
    cursor: pointer;
  }
  .el-icon {
    margin-left: 5px;
  }
}
::v-deep .el-form-item__label {
  font-size: 16px;
  color: #cbe7ff;
}
.el-input-group__append {
  background-color: rgba(124, 173, 216, 0.2);
  border-left: 1px solid;
  border-color: #7cadd8;
  color: #cbe7ff;

  .el-icon-location-outline {
    font-size: 15px;
    color: #fff;
  }
}
.miBaseForm {
  display: flex;
  flex-wrap: wrap;
  padding: 17px;
  ::v-deep .el-form-item {
    width: 33.33333%;
    flex: 0 0 33.33333%;
  }
}
::v-deep .el-form-item__label {
  line-height: 32px;
}
::v-deep .el-input__inner {
  height: 32px;
  line-height: 32px;
  background-color: transparent;
  border-color: rgba(124, 173, 216, 1);
  &::placeholder {
    color: #7cadd8;
  }
}
::v-deep .el-form-item__content {
  line-height: 32px;
}
::v-deep .el-button {
  color: #fff;
  height: 32px;
  padding: 0 20px;
}
.dialogBox-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  height: 474px;
}
.dialogBox-box-left {
  width: 50%;
}
.dialogBox-box-right {
  width: 50%;
}
.treeBox {
  height: 406px;
  overflow: auto;
}
.title {
  color: #fff;
}
.el-tree {
  widht: 100%;
  background-color: transparent;
  color: rgba(255, 255, 255, 0.9);
}
// 重置dialog样式
.el-dialog {
  background: transparent;

  .el-dialog__header {
    background: rgba(6, 50, 88, 0.9);
    padding: 20px 0 0 20px;
    height: 24px;
    line-height: 24px;
  }

  .el-dialog__title {
    // color: #313335;
    color: #ffffff;
    font-size: 16px;
    font-weight: bold;
  }

  .el-dialog__headerbtn {
    top: 20px;
    right: 20px;
    height: 14px;
    line-height: 14px;

    .el-dialog__close {
      // color: #354052;
      color: #4af6fc;
      width: 14px;
      height: 14px;
    }
  }

  .el-dialog__body {
    width: calc(100% - 40px);
    padding: 30px 20px 20px;
    // font-size: 0.16px;
    font-size: 0.18px;
    color: #313335;
    background: rgba(6, 50, 88, 0.9);
  }
  .el-dialog__footer {
    background: rgba(6, 50, 88, 0.9) !important;
  }
}
.el-tree-node:focus > .el-tree-node__content {
  background: rgba(74, 246, 252, 0.1);
  box-shadow: inset 0px 0px 10px 0px #4af6fc;
}
.el-tree-node__content:hover {
  background: rgba(74, 246, 252, 0.1);
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  background: rgba(74, 246, 252, 0.1);
  box-shadow: inset 0px 0px 10px 0px #4af6fc;
}
.chengyuanxiangqing {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  background: url('~@/assets/TeamAdministrator/icon_folder_shut(3).png');
}
.zhedie {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  background: url('~@/assets/TeamAdministrator/icon_folder_shut(2).png');
}
.zhankai {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  background: url('~@/assets/TeamAdministrator/icon_folder_shut(1).png');
}
.geren {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  background: url('~@/assets/TeamAdministrator/icon_folder_shut.png');
}
.dialogBox-box-right {
  border-left: 1px solid #1d5a8f;
  padding-left: 8px;
}
.dialogBox-box-left {
  padding-right: 8px;
}
.el-tree > .el-tree-node {
  display: inline-block;
  min-width: 100%;
}
.binanBox {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.title {
  color: #fff;
}
.tianjiachengyuan {
  margin-bottom: 15px;
  color: #4af6fc;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.xuanzechengyuanBox {
  display: flex;
  justify-content: space-between;
}
.el-form-item__label-wrap{ float: left; }
</style>
